module cl {
	export class BombAction extends ActionPerformer {
		constructor(
				self_strength : number,
				self_morale : number,
				tgt_strength : number,
				tgt_morale : number) {
			super();
			this.type = 3;
			this.paramKeys = JSON.stringify(["self_strength","self_morale","tgt_strength","tgt_morale"]);
			this.paramVals = JSON.stringify([self_strength,self_morale,tgt_strength,tgt_morale]);
		}

		public accept() : void {

			let param = ActionUtil.getParamJSON(this);

			let troop = Campaign.troop[this.troopid];
			let targetTroopid = this.other_troopids[0];
			let targetTroop = Campaign.troop[targetTroopid];

			// 看见目标区域
			ActionUtil.setAction(new SeeAction(troop.force), this.troopid, this.move_remain, this.tid).accept();

			// 数据
			troop.move_remain = this.move_remain;
			targetTroop.move_remain = this.other_m_remain[0];

			troop.strength = param["self_strength"] as number;
			targetTroop.strength = param["tgt_strength"] as number;

			troop.morale = param["self_morale"] as number;
			targetTroop.morale = param["tgt_morale"] as number;
		}

		public acceptOnce() : void { this.accept(); }

		public duration() : number {
			return 3000;
		}

		public play(map : CampaignPage) : void {

			let param = ActionUtil.getParamJSON(this);

            let troop = Campaign.troop[this.troopid];
            let targetTroopid = this.other_troopids[0];
            let targetTroopGroup = map.groupTroop.getChildByName(String(targetTroopid)) as eui.Group;

            let troopGroup = map.groupCourtTroop.getChildByName(String(this.troopid)) as eui.Group;
            map.groupCourtTroop.removeChild(troopGroup);

            let coord0 = Map.tileEngine.tile2coord(troop.force_tile);
            let coord1 = Map.tileEngine.tid2coord(this.tid);

			// 看见目标区域
            ActionUtil.setAction(new SeeAction(troop.force), this.troopid, this.move_remain, this.tid).play(map);

            // 动作动画
            let blast = Util.setImageCoord("under_attack_png", new Coord(targetTroopGroup.x, targetTroopGroup.y));

            let flyEffect = new FlyEffect(map, troopGroup, coord0, coord1,
                    () : void => {
                        // 爆炸
                        map.groupEffect.addChild(blast);
                        // 震动
                        AttackEffect.shake(targetTroopGroup.getChildAt(1), 16, 350, 320);
                        let intervalCount = 0;
                        let intervalHandle = setInterval(()=>{
                            if (intervalCount == 0) {
                                map.groupEffect.addChild(blast);

                            } else if (intervalCount == 1) {
                                map.groupEffect.removeChild(blast);

                            } else if (intervalCount == 2) {
                                clearInterval(intervalHandle);
                                // 血条
                                map.troopManager.drawBloodSymbol(troopGroup, param["self_strength"] as number, param["self_morale"] as number);
                                map.troopManager.drawBloodSymbol(targetTroopGroup, param["tgt_strength"] as number, param["tgt_morale"] as number);
                                // 行动力
                                MoveRemainEffect.play(troopGroup, this.move_remain);
                                MoveRemainEffect.play(targetTroopGroup, this.other_m_remain[0]);
                            }
                            intervalCount++;
                        }, 350);
                    },
                    () : void => {
                        map.troopManager.setCourtTroopLocation(troop, troopGroup);
                        Util.setOrientation(troopGroup.getChildAt(1), troop.orientation);
                        map.groupCourtTroop.addChild(troopGroup);
                    });
            flyEffect.start();
        }
	}
}